package com.bh.extend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.utils.DateUtils;
import com.bh.extend.domain.JcAbnormal;
import com.bh.extend.domain.JcAbnormalDetail;
import com.bh.extend.mapper.JcAbnormalDetailMapper;
import com.bh.extend.mapper.JcAbnormalMapper;
import com.bh.extend.service.IJcAbnormalService;
import com.bh.kysys.domain.JcSysOrganization;
import com.bh.kysys.mapper.JcSysOrganizationMapper;
import com.bh.order.domain.JcOrder;
import com.bh.order.mapper.JcOrderMapper;
import com.bh.shipment.domain.JcShipment;
import com.bh.shipment.mapper.JcShipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 异常Service业务层处理
 *
 * @author slx
 * @date 2023-04-24
 */
@Service
public class JcAbnormalServiceImpl extends ServiceImpl<JcAbnormalMapper, JcAbnormal> implements IJcAbnormalService {
    @Autowired
    private JcAbnormalMapper jcAbnormalMapper;
    @Autowired
    private JcAbnormalDetailMapper jcAbnormalDetailMapper;
    @Autowired
    private JcOrderMapper jcOrderMapper;
    @Autowired
    private JcShipmentMapper jcShipmentMapper;
    @Autowired
    private JcSysOrganizationMapper jcSysOrganizationMapper;
    /**
     * 查询异常
     *
     * @param id 异常主键
     * @return 异常
     */
    @Override
    public JcAbnormal selectJcAbnormalById(Long id) {
        return jcAbnormalMapper.selectById(id);
    }

    /**
     * 查询异常列表
     *
     * @param jcAbnormal 异常
     * @return 异常
     */
    @Override
    public List<JcAbnormal> selectJcAbnormalList(JcAbnormal jcAbnormal) {
        List<JcAbnormal>  abnormals = jcAbnormalMapper.selectJcAbnormalList(jcAbnormal);
        for (JcAbnormal abnormal : abnormals) {
            List<JcAbnormalDetail> jcAbnormalDetails = jcAbnormalDetailMapper.selectJcAbnormalDetailListByAbnormalId(abnormal.getId());
            abnormal.setJcAbnormalDetailList(jcAbnormalDetails);
            JcOrder jcOrder = jcOrderMapper.selectById(abnormal.getJcOrderId());
            if (jcOrder != null) {
                abnormal.setJcOrder(jcOrder);
            }

            JcShipment jcShipment = jcShipmentMapper.selectById(abnormal.getJcShipmentId());
            if (jcShipment != null) {
                abnormal.setJcShipment(jcShipment);
            }

            JcSysOrganization jcSysOrganization = jcSysOrganizationMapper.selectById(abnormal.getJcOrganizationId());
            if (jcSysOrganization != null) {
                abnormal.setJcOrganizationName(jcSysOrganization.getName());
            }
        }
        return abnormals;
    }

    /**
     * 新增异常
     *
     * @param jcAbnormal 异常
     * @return 结果
     */
    @Override
    public int insertJcAbnormal(JcAbnormal jcAbnormal) {
        jcAbnormal.setCreateTime(DateUtils.getNowDate());
        return jcAbnormalMapper.insert(jcAbnormal);
    }

    /**
     * 修改异常
     *
     * @param jcAbnormal 异常
     * @return 结果
     */
    @Override
    public int updateJcAbnormal(JcAbnormal jcAbnormal) {
        return jcAbnormalMapper.updateById(jcAbnormal);
    }

    /**
     * 批量删除异常
     *
     * @param ids 需要删除的异常主键
     * @return 结果
     */
    @Override
    public int deleteJcAbnormalByIds(Long[] ids) {
        return jcAbnormalMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除异常信息
     *
     * @param id 异常主键
     * @return 结果
     */
    @Override
    public int deleteJcAbnormalById(Long id) {
        return jcAbnormalMapper.deleteById(id);
    }
}
